package com.lyncs.ods.modules.txn.controller;


import com.lyncs.ods.common.api.CommonPage;
import com.lyncs.ods.common.api.CommonResult;
import com.lyncs.ods.modules.txn.service.DeliverService;
import com.lyncs.ods.req.ListPageSearchReq;
import com.lyncs.ods.req.SaveDeliverReq;
import com.lyncs.ods.resp.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 交付单总表 前端控制器
 * </p>
 *
 * @author alex
 * @since 2022-02-15
 */
@RestController
@RequestMapping("/txn/deliver")
@Api(tags = {"交付相关接口"})
public class DeliverController {


    @Autowired
    private DeliverService deliverService;

    @PostMapping("save")
    @ApiOperation(value = "创建/修改 交付单")
    public CommonResult<Long> saveDeliver(@Valid @RequestBody SaveDeliverReq req) {
        return CommonResult.success(deliverService.saveDeliver(req));
    }

    @PostMapping("list")
    @ApiOperation(value = "查询 交付单列表")
    public CommonResult<CommonPage<ListPageResp>> listDeliver(@RequestBody ListPageSearchReq req) {
        return CommonResult.success(deliverService.listDeliver(req));
    }

    @GetMapping("{deliverId}/detail")
    @ApiOperation(value = "查询 交付单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deliverId", value = "交付单ID", dataType = "Long", paramType = "path"),
    })
    public CommonResult<DeliverDetailInfoResp> getDeliverDetail(@PathVariable Long deliverId) {
        return CommonResult.success(deliverService.getDeliverDetail(deliverId));
    }

    @GetMapping("{deliverId}/top")
    @ApiOperation(value = "置顶/取消置顶")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deliverId", value = "交付单ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "操作类型:0.取消置顶;1.置顶", dataType = "Integer", paramType = "query"),
    })
    public CommonResult<?> stickyOnTop(@PathVariable Long deliverId, @RequestParam Integer type) {
        deliverService.stickyOnTop(deliverId, type);
        return CommonResult.success();
    }

    @GetMapping("{deliverId}/reverse")
    @ApiOperation(value = "撤销交付单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deliverId", value = "交付单ID", dataType = "Long", paramType = "path"),
    })
    public CommonResult<?> reverseDeliver(@PathVariable Long deliverId) {
        deliverService.reverseDeliver(deliverId);
        return CommonResult.success();
    }

    @GetMapping("listUndeliveredOrder")
    @ApiOperation(value = "获取未完全交付订单中的SKU信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "seller_id", value = "卖方公司id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "buyer_id", value = "买方公司id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "currency", value = "请求订单的币种", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "order_id", value = "跳转订单id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页数，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "page_size", value = "当前页请求数量，默认5", dataType = "Integer", paramType = "query"),
    })
    public CommonResult<CommonPage<UndeliveredOrderSkuResp>> listUndeliveredOrder(@RequestParam("seller_id") Long sellerId,
                                                                                  @RequestParam("buyer_id") Long buyerId,
                                                                                  @RequestParam(value = "currency") String currency,
                                                                                  @RequestParam(value = "order_id", required = false) Long orderId,
                                                                                  @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                                  @RequestParam(value = "page_size", required = false, defaultValue = "5") Integer pageSize) {
        return CommonResult.success(deliverService.listUndeliveredOrder(sellerId, buyerId, currency, orderId, page, pageSize));
    }

    @GetMapping("{deliverId}/editLog")
    @ApiOperation(value = "交付单变更历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deliverId", value = "交付单ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "page", value = "当前页数，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "page_size", value = "当前页请求数量，默认20", dataType = "Integer", paramType = "query"),
    })
    public CommonResult<CommonPage<TxnEditLogResp>> getDeliverEditLog(@PathVariable Long deliverId,
                                                                      @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                      @RequestParam(value = "page_size", required = false, defaultValue = "20") Integer pageSize) {
        return CommonResult.success(deliverService.getDeliverEditLog(deliverId, page, pageSize));
    }

    @GetMapping("{deliverId}/log")
    @ApiOperation(value = "交付单整单历史记录")
    public CommonResult<DeliverLogResp> getDeliverLog(@PathVariable Long deliverId) {
        return CommonResult.success(deliverService.getDeliverLog(deliverId));
    }


    @GetMapping("{deliverId}/{billId}/settleLog")
    @ApiOperation(value = "交付单中某账款结算历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deliverId", value = "交付单ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "billId", value = "账款ID", dataType = "Long", paramType = "path"),
    })
    public CommonResult<DeliverSettleLogResp> getDeliverSettleLog(@PathVariable("deliverId") Long deliverId, @PathVariable("billId") Long billId) {
        return CommonResult.success(deliverService.getDeliverSettleLog(deliverId, billId));
    }

    @GetMapping("{deliverId}/{skuId}/deliverLog")
    @ApiOperation(value = "交付单某sku交付历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deliverId", value = "交付单ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "skuId", value = "sku ID", dataType = "Long", paramType = "path"),
    })
    public CommonResult<DeliverSkuLogResp> getOrderSkuDeliverLog(@PathVariable("deliverId") Long deliverId, @PathVariable("skuId") Long skuId) {
        return CommonResult.success(deliverService.getSkuDeliverLog(deliverId, skuId));
    }

}

